﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


// Condition: Write a program that finds the index of given elements
//            in a sorted array of integers by using the binary search
//            algorithm.

class BinarySearch
{
    static void Main()
    {
        int[] myArr = { 0, 5, 13, 19, 22, 41, 55, 68, 72, 81, 98 };
        Console.WriteLine("Given sorted array:");
        for (int i = 0; i < myArr.Length; i++)
        {
            Console.Write(myArr[i]+" ");
        }
        Console.WriteLine();
        Console.Write("Enter one element of given array: ");
        int target = int.Parse(Console.ReadLine());
        int lo = 0;
        int hi = myArr.Length - 1;


        while (lo <= hi)
        {
            int mid = (hi + lo) / 2;
            if (myArr[mid] == target)
            {
                Console.WriteLine("The index of number {0} is: {1}", target, mid);

                break;
            }
            else if (myArr[mid] < target)
            {
                lo = mid + 1;
            }
            else
            {
                hi = mid - 1;
            }

        }
    }
}

